# author ： 以凝
# time   ： 2024/3/17  22:06
import os.path
from pathlib import Path

from PyQt5.QtCore import Qt
from PyQt5.QtGui import QFont
from PyQt5.QtWidgets import QTreeView, QFileSystemModel, QWidget, QVBoxLayout, QDockWidget, QSplitter, QTextEdit, \
    QListWidget, QMainWindow

from libs import settings
from libs.canvas import Canvas
from libs.constants import SETTING_DRAW_SQUARE
from libs.message import Message
from libs.settings import Settings
from libs.stringBundle import StringBundle
from libs.utils.action import add_actions
from libs.toolBar import ToolBar


'''
 这是这个软件模型的基本骨架
'''
class WindowMixin(QMainWindow):
    def __init__(self):
        self.modal1_name = None
        self.modal2_name = None
        super().__init__()
        self.setup_settings()
        self.file_explorer()
        self.setup_status_box()
        self.setup_canvas()
        self.setup_dat_file_property()
        self.tabifyDockWidget(self.filesys, self.property_dock)
        self.filesys.raise_()

    def setup_dat_file_property(self):
        '''
          dat文件附加属性
         '''
        list_layout = QVBoxLayout()
        list_layout.setContentsMargins(0, 0, 0, 0)
        self.property_list = QListWidget()
        property_list_container = QWidget()
        property_list_container.setLayout(list_layout)
        list_layout.addWidget(self.property_list)
        self.property_dock = QDockWidget(StringBundle.get_bundle().get_string('datProperty'), self)
        self.property_dock.setWidget(property_list_container)
        self.addDockWidget(Qt.RightDockWidgetArea, self.property_dock)


    def setup_settings(self):
        # Load setting in the main thread. The setting info is placed in config
        self.settings = Settings()
        self.settings.load()


    def setup_canvas(self):
        self.canvas_modal1 = Canvas(self)
        self.canvas_modal2 = Canvas(self)
        self.canvas_modal1.set_drawing_shape_to_square(self.settings.get(SETTING_DRAW_SQUARE, False))

    def menu(self, title, actions=None):
        menu = self.menuBar().addMenu(title)
        if actions:
            add_actions(menu, actions)
        return menu

    def toolbar(self, title, actions=None):
        toolbar = ToolBar(title)
        toolbar.setObjectName(u'%sToolBar' % title)
        toolbar.setToolButtonStyle(Qt.ToolButtonTextUnderIcon)
        if actions:
            add_actions(toolbar, actions)
        self.addToolBar(toolbar)
        return toolbar

    def file_explorer(self):
        # 创建文件系统模型
        self.modal1_file_modal = QFileSystemModel()  # 模态1所在的文件夹
        self.modal2_file_modal = QFileSystemModel()  # 模态2所在的文件夹

        # 创建树视图，设置模型
        self.tree1 = QTreeView()
        self.tree1.setModel(self.modal1_file_modal)
        self.tree1.setHeaderHidden(True)  # 隐藏标题栏

        self.tree2 = QTreeView()
        self.tree2.setModel(self.modal2_file_modal)
        self.tree2.setHeaderHidden(True)  # 隐藏标题栏

        # 创建一个splitter用于上下布局两个树视图
        splitter = QSplitter(Qt.Vertical)
        splitter.addWidget(self.tree1)
        splitter.addWidget(self.tree2)

        # 创建dock widget
        self.filesys = QDockWidget("文件树", self)
        self.filesys.setObjectName("FileExplorerDockWidget")

        # 设置dock widget的内容为splitter
        self.filesys.setWidget(splitter)

        # 将dock widget添加到主窗口的右侧
        self.addDockWidget(Qt.RightDockWidgetArea, self.filesys)

    def set_file_explorer_root_path(self, root_path , modal = 1 ):
        # 设置文件系统模型根路径
        if modal == 1 :
            self.modal1_name = Path(root_path).name
            self.modal1_file_modal.setRootPath(root_path)
            self.tree1.setRootIndex(self.modal1_file_modal.index(root_path))
        elif modal == 2 :
            self.modal2_name = Path(root_path).name
            self.modal2_file_modal.setRootPath(root_path)
            self.tree2.setRootIndex(self.modal2_file_modal.index(root_path))
        else :
            raise Exception("Wrong modal. You should specify the modal is 1 or 2")
        print("self.modal1" , self.modal1_name)
        print("self.modal2" , self.modal2_name)


    def setup_status_box(self):
        # 创建状态信息框
        self.status_box = QTextEdit()
        self.status_box.setFontWeight(QFont.Normal)
        self.status_box.setReadOnly(True)  # 设置为只读，用户不能编辑内容

        font = self.status_box.font()
        font.setPointSize(12)  # 增大字体大小，你可以根据需要调整这个值
        self.status_box.setFont(font)

        # 添加状态信息框到主窗口的底部
        self.status_dock_widget = QDockWidget("信息框", self)
        self.status_dock_widget.setObjectName("StatusBoxDockWidget")
        self.status_dock_widget.setWidget(self.status_box)

        self.addDockWidget(Qt.BottomDockWidgetArea, self.status_dock_widget)

    def append_status_message(self, message , is_prompt = False):
        message = Message.make_message(message,is_prompt)
        # 将带格式的消息追加到状态信息框的末尾
        self.status_box.insertHtml(message + "<br>")  # 每条消息后添加换行
        self.status_box.ensureCursorVisible()  # 确保最新消息处在可视范围内